package com.example.android.ui.message.Adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.android.R;
import com.example.android.bean.Message;
import com.example.android.utils.CircleImageView;

import java.util.List;


public class MessageRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    private RecyclerView recyclerView;
    private List<Message> messageList;
    private LayoutInflater layoutInflater;
    private Context context;
    private OnItemClickListener listener;

    public MessageRecyclerViewAdapter(RecyclerView recyclerView, Context context, List<Message> messageList) {
        this.recyclerView = recyclerView;
        this.messageList = messageList;
        layoutInflater = LayoutInflater.from(context);
        this.context = context;
    }

    public void setMessageList(List<Message> messageList) {
        this.messageList.clear();
        this.messageList.addAll(messageList);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.message_item, parent, false);
        view.setOnClickListener(this
//                new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                int position = recyclerView.getChildAdapterPosition(view);
//                Log.i("MessageActivity","点击item"+position+" "+messageList.get(position).getUserId());
//            }
//        }
        );
        return new MessageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        MessageViewHolder messageViewHolder = (MessageViewHolder) holder;
        messageViewHolder.name.setText(messageList.get(position).getUserName());
        messageViewHolder.latestMessage.setText(messageList.get(position).getLatestMessage());
        messageViewHolder.date.setText(messageList.get(position).getDate());
        Glide.with(context)
                .load(messageList.get(position).getUserImage())
                .apply(RequestOptions.centerCropTransform())
                .into(messageViewHolder.image);

    }

    @Override
    public int getItemCount() {
        return messageList.size();
    }

    @Override
    public void onClick(View view) {
        if (listener != null) {
            int position = recyclerView.getChildAdapterPosition(view);
            listener.onItemClick(messageList.get(position));
        }
    }

    class MessageViewHolder extends RecyclerView.ViewHolder {
        CircleImageView image;
        TextView name, latestMessage, date;

        public MessageViewHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.message_item_username);
            latestMessage = itemView.findViewById(R.id.message_item_latestmessage);
            date = itemView.findViewById(R.id.message_item_date);
            image = itemView.findViewById(R.id.message_item_image);
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(Message message);
    }
}
